package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.pojo.dto.FixDataJobDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.workflow.domain.dao.WorkflowTransferDataDao;
import com.xbongbong.workflow.domain.entity.WorkflowTransferDataEntity;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/09 15:55
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/workflow")
public class WorkflowTransferController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowTransferController.class);

    @Resource
    private WorkflowTransferDataDao workflowTransferDataDao;
    @Resource
    private ProBaseConfig proBaseConfig;

    @RequestMapping(value = "/transfer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("del", 0);
        param.put("formId", 0);
        Integer allTotal = workflowTransferDataDao.getEntitiesCount(param);
        LOGGER.info("共：" + allTotal + "条");
        Integer pageSize = 100;
        Integer index = allTotal/pageSize + 1;
        Long page = 0L;
        List<WorkflowTransferDataEntity> list = null;
        for (int j = 0; j < index; j++) {
            param.clear();
            param.put("idGte", page);
            param.put("pageSize", pageSize);
            param.put("del", 0);
            param.put("orderByStr", "id");
            list = workflowTransferDataDao.findEntities(param);
            if (CollectionsUtil.isNotEmpty(list)) {
                Integer total = list.size();
                page = list.get(total-1).getId();
                List<WorkflowTransferDataEntity> resultList = new ArrayList<>();

                list.forEach(item->{
                    WorkflowTransferDataEntity workflowTransferDataEntity = new WorkflowTransferDataEntity();
                    WorkflowTransferPOJO workflowTransferPOJO =  JSON.parseObject(item.getData().toJSONString(), WorkflowTransferPOJO.class);
                    Data data = workflowTransferPOJO.getData();
                    if (Objects.nonNull(data)) {
                        workflowTransferDataEntity.setId(item.getId());
                        workflowTransferDataEntity.setFormId(data.getFormId());
                        workflowTransferDataEntity.setSaasMark(data.getSaasMark());
                        workflowTransferDataEntity.setBusinessType(data.getBusinessType());
                        workflowTransferDataEntity.setDataId(data.getId());
                        workflowTransferDataEntity.setOperate(workflowTransferPOJO.getOperate());
                        resultList.add(workflowTransferDataEntity);
                    }
                });
                workflowTransferDataDao.updateBatch(resultList, null);
            }
        }
        return "success";
    }



}
